const { query } = require('../../../lib/db');
const { authenticateToken } = require('../../../lib/auth');

async function handler(req, res) {
  if (req.method !== 'GET') {
    return res.status(405).json({
      success: false,
      message: '方法不允许'
    });
  }

  try {
    // 查询所有启用的租户
    const tenants = await query(
      'SELECT id, name, code, description FROM tenants WHERE status = 1 ORDER BY name',
      []
    );

    res.status(200).json({
      success: true,
      data: tenants
    });

  } catch (error) {
    console.error('租户列表错误:', error);
    res.status(500).json({
      success: false,
      message: '服务器内部错误'
    });
  }
}

export default authenticateToken(handler);